package cmp.compile.panels;

/************************************************
 * Used by all methods that need to select a file
 */
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretListener;

import cmp.compile.CompileMain;
import cmp.main.Globals;

public class FileSelectTextField extends JPanel {
	private static final long serialVersionUID = 2799665158668961271L;
	/**
	 * Acts like JTextField but includes a button for file selection
	 * @param parentPanel controlling panel
	 * @param textSize size of the JTextField
	 * @param startDir Null for current project directory, otherwise static starting dir
	 */
	public FileSelectTextField(final CompilePanel parentPanel, int textSize, final String startDir, String type) { 

		theParentPanel = parentPanel;
		theType = type;
		txtField = new JTextField(textSize);
		txtField.setMaximumSize(txtField.getPreferredSize());
		txtField.setMinimumSize(txtField.getPreferredSize());
		
		btnSelectFile = new JButton("...");
		btnSelectFile.setBackground(Globals.BGCOLOR);
		btnSelectFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fc = new JFileChooser();
				String startingDir = startDir;
				if(startingDir == null) {
					if (theType.equals("blast"))
					{
						startingDir = theParentPanel.pnlBlast.getDefaultBlastDirFullPath();
					}
					else startingDir = theParentPanel.getCurrentProjectDirectory();
				}
				fc.setCurrentDirectory(new File(startingDir));
				
				if(fc.showOpenDialog(theParentPanel) == JFileChooser.APPROVE_OPTION) {
					String filePath = fc.getSelectedFile().getAbsolutePath();
					if (theType.equals("blast")) {
						String blastRelPath = theParentPanel.pnlBlast.getDefaultBlastDirRelPath();
						if (filePath.contains(blastRelPath))
						{
							int index = filePath.indexOf(blastRelPath);
							txtField.setText(filePath.substring(index));
						}
						else txtField.setText(fc.getSelectedFile().getAbsolutePath());
					}
					else {
						// Called for getting SMAT file or proteinFile
						if(filePath.contains(startingDir)) {
							int index = filePath.lastIndexOf("/"); // CAS 3/29/13
							String aaDir = Globals.Compile.AAfiles;
							if (filePath.contains(aaDir)) {
								txtField.setText(aaDir + "/" + filePath.substring(index+1));
							}
							else {
								txtField.setText(filePath.substring(index + 1));
							}
						}
						else
							txtField.setText(fc.getSelectedFile().getAbsolutePath());
					}
				}
			}
		});
		btnSelectFile.setPreferredSize(new Dimension(btnSelectFile.getPreferredSize().width, txtField.getPreferredSize().height));
		btnSelectFile.setMaximumSize(btnSelectFile.getPreferredSize());
		btnSelectFile.setMinimumSize(btnSelectFile.getPreferredSize());
		
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		setBackground(Globals.BGCOLOR);
		
		add(txtField);
		add(btnSelectFile);
		
		setMaximumSize(getPreferredSize());
		setMinimumSize(getPreferredSize());
	}
	
	public void addActionListener(ActionListener l) { btnSelectFile.addActionListener(l); }
	public void addFocusListener(FocusListener l) { 
		txtField.addFocusListener(l);
		btnSelectFile.addFocusListener(l);
	}
	public void addCaretListener(CaretListener l) { txtField.addCaretListener(l); }
	
	public void setText(String text) { 
		txtField.setText(text); 
	}
	public String getText() { return txtField.getText(); }
	
	public void setEnabled(boolean enabled) {
		txtField.setEnabled(enabled);
		btnSelectFile.setEnabled(enabled);
	}
	
	private CompilePanel theParentPanel = null;
	private JTextField txtField = null;
	private JButton btnSelectFile = null;
	private String theType = "";
}
